package com.seeyon.apps.kk.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub.IFGW001_CN_GW_REQ_DT;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub.IFGW001_CN_GW_REQ_MT;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub.IFGW001_CN_GW_RES_MT;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub.ITEM_type0;
import com.locknlock.gw.ifgw001.IFGW001_CN_GW_SOServiceStub.RETURN_type0;
import com.seeyon.apps.kk.manager.CaseSaveManagerImpl;
import com.seeyon.apps.kk.manager.JSONKKConfigManager;
import com.seeyon.apps.kk.manager.KKMemberHeadImageManagerImpl;
import com.seeyon.apps.kk.manager.KkConfigManager;
import com.seeyon.apps.kk.manager.OrgSyncManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;
import com.seeyon.kk.utils.UploadUtils;

@NeedlessCheckLogin
public class OrgSyncController extends KkBaseController {
	private static final Log log = LogFactory.getLog(OrgSyncController.class);

	private OrgSyncManager orgSyncManager;

	private JSONKKConfigManager configManager = (JSONKKConfigManager) AppContext.getBean("JSONKKConfigManager");

	public OrgSyncManager getOrgSyncManager() {
		return orgSyncManager;
	}

	public void setOrgSyncManager(OrgSyncManager orgSyncManager) {
		this.orgSyncManager = orgSyncManager;
	}

	private CaseSaveManagerImpl caseSaveManagerImpl;

	public CaseSaveManagerImpl getCaseSaveManagerImpl() {
		return caseSaveManagerImpl;
	}

	public void setCaseSaveManagerImpl(CaseSaveManagerImpl caseSaveManagerImpl) {
		this.caseSaveManagerImpl = caseSaveManagerImpl;
	}

	private KKMemberHeadImageManagerImpl memberHeadImageManagerImpl;

	public KKMemberHeadImageManagerImpl getMemberHeadImageManagerImpl() {
		return memberHeadImageManagerImpl;
	}

	public void setMemberHeadImageManagerImpl(KKMemberHeadImageManagerImpl memberHeadImageManagerImpl) {
		this.memberHeadImageManagerImpl = memberHeadImageManagerImpl;
	}

	public ModelAndView handSync(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = this.orgSyncManager.kkSaveSync();
		if (StringUtils.equals("1000", MapUtils.getString(map, "code")))
			return this.ajaxDoneSuccess("同步成功", JSON.toJSONString(map));
		else
			return this.ajaxDoneError("同步失败", JSON.toJSONString(map));
	}

	public ModelAndView updateImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, String> map = memberHeadImageManagerImpl.updateMenberHeadImage();
		if (StringUtils.equals("1000", MapUtils.getString(map, "code")))
			return this.ajaxDoneSuccess("个人照片更新成功", JSON.toJSONString(map));
		else
			return this.ajaxDoneError("个人照片更新失败", JSON.toJSONString(map));
	}

	/**
	 * 打开起案正文页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView showUeditor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ROOT_PATH + "/json/index");
		return mv;
	}

	/**
	 * 打开查看起案正文页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView catUeditor(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ROOT_PATH + "/json/casePreview");
		return mv;
	}

	public ModelAndView showPrint(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ROOT_PATH + "/json/printOrPreview");
		return mv;
	}

	/**
	 * 起案正文图片上传
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		DefaultMultipartHttpServletRequest ooo = (DefaultMultipartHttpServletRequest) request;
		Map<String, MultipartFile> map = ooo.getFileMap();
		String path = UploadUtils.upload(map.get("file"));
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/tinymceUrl");
		mav.addObject("url", path);
		return mav;
	}

	public ModelAndView uploadCaseImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		DefaultMultipartHttpServletRequest ooo = (DefaultMultipartHttpServletRequest) request;
		Map<String, MultipartFile> map = ooo.getFileMap();
		String path = UploadUtils.uploadCase(map.get("file"));
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/tinymceUrl");
		mav.addObject("url", path);
		return mav;
	}

	/**
	 * 起案保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView saveCase(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<>();
		map.put("id", request.getParameter("id"));
		map.put("text", request.getParameter("text"));
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/saveCaseStatus");
		mav.addObject("status", caseSaveManagerImpl.saveCase(map));
		return mav;
	}

	/**
	 * 获取起案正文
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getCaseContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.addHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<>();
		map.put("id", request.getParameter("id"));
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/saveCaseStatus");
		mav.addObject("status", caseSaveManagerImpl.getCaseContext(map));
		return mav;
	}

	/**
	 * 获取翻译配置文件
	 */
	public ModelAndView getLanguageConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/contentJson");
		mav.addObject("status", configManager.getJson());
		return mav;
	}

	/**
	 * 获取内容正文翻译
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getContentConfig(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView(ROOT_PATH + "/json/contentJson");
		mav.addObject("status", configManager.getContentJson());
		return mav;
	}

	public void ldapLink(HttpServletRequest request, HttpServletResponse response) {
		try {
			String javaHome = System.getProperty("java.home");
			String keystore = javaHome + "/lib/security/cacerts";
			System.setProperty("javax.net.ssl.trustStore", keystore);

			Properties env = new Properties();
			String adminName = "administrator";
			String adminPassword = "lock%!^@@*lock";// password
			String ldapURL = "ldaps://CHNAWS-BDCS001.locknlock.com.cn:636";// ip:port
			env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.SECURITY_AUTHENTICATION, "simple");// LDAP访问安全级别："none","simple","strong"
			env.put(Context.SECURITY_PRINCIPAL, adminName);
			env.put(Context.SECURITY_CREDENTIALS, adminPassword);
			env.put(Context.PROVIDER_URL, ldapURL);
			env.put(Context.SECURITY_PROTOCOL, "ssl");
			response.getWriter().println("java 证书url:" + keystore);
			LdapContext lc = new InitialLdapContext(env, null);
			response.getWriter().println("AD域ssl身份认证成功");
		} catch (Exception e) {
			try {
				response.getWriter().println("AD域ssl身份认证出错:" + e.getMessage());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	/**
	 * 测试考勤
	 * @param request
	 * @param response
	 */
	public void sendGHR(HttpServletRequest request, HttpServletResponse response){
		try {
			IFGW001_CN_GW_SOServiceStub stu = new IFGW001_CN_GW_SOServiceStub();
			HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
			auth.setUsername("IFPO01");
			auth.setPassword("Passpo1$");
			
		
			stu._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);
			IFGW001_CN_GW_REQ_MT iFGW001_CN_GW_REQ_MT0 = new IFGW001_CN_GW_REQ_MT();
			IFGW001_CN_GW_REQ_DT param=new IFGW001_CN_GW_REQ_DT();
			ITEM_type0[] sapV2 = new ITEM_type0[1];
			
			ITEM_type0 ii = new ITEM_type0();
			ii.setSRC_TRX_ID("55465513546");//邮箱ID
			ii.setEMPNO("200003255422");//员工编号
			ii.setSEQ("123");//SEQID
			ii.setATT_TYPE("2");//考勤类型 为2
			
			ii.setBGN_TIME("2020-05-25");//考勤开始时间
			ii.setEND_TIME( "2020-05-25");
			ii.setPUNCH_DATE("2020-05-25");//考勤日期
		
			ii.setREMARKS("2020-05-25");//事由
			ii.setGHR_STATUS("N");
			
			sapV2[0]=ii;
			param.setITEM(sapV2);
			iFGW001_CN_GW_REQ_MT0.setIFGW001_CN_GW_REQ_MT(param);
			IFGW001_CN_GW_RES_MT ifgw001_CN_GW_SO = stu.IFGW001_CN_GW_SO(iFGW001_CN_GW_REQ_MT0);
			RETURN_type0[] as = ifgw001_CN_GW_SO.getIFGW001_CN_GW_RES_MT().getRETURN();
			
			response.getWriter().println(as[0].getMESSAGE());
			response.getWriter().println(as[0].getSTATUS());
			response.getWriter().println(as[0].getIF_DATE());
		} catch (Exception e) {
			try {
				response.getWriter().println("error:"+e.getMessage());
				response.getWriter().println("error:"+e);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
